<?php
namespace app\api\controller\deerhome\user;

use app\api\controller\deerhome\user\BaseAuth;
use think\Db;
use think\Validate;
use app\admin\model\deerhome\ShopApply as ShopApplyModel;

/**
 * 门店入驻申请
 */
class ApplyShop extends BaseAuth
{
    
    public function index()
    {
        $this->xcxLog("门店入驻页");
        $data=[];
        $data['tel']=$this->_user['utel'];
        
        $base=\file_get_contents(APP_PATH."../public/assets/addons/deerhome/city.json");
        $base=json_decode($base,true);

        $cityData=Db::name("deerhome_open_city")->select();
        foreach($cityData as $k=>$v){
            $cityData[$k]['label']=$v['cityname'];
            $cityData[$k]['children']=$this->getCityArea($base,$v['cityname'],$v['province']);
        }
        $data['city']=$cityData;
        $this->ok($data);
    }
    private function getCityArea($data,$cityname,$province)
    {
        $area=[];
        $provinceCode="";
        //找出省份的code
        foreach($data['86'] as $k=>$v){
            foreach($v as $k2=>$v2){
                if($v2['address']==$province){
                    $provinceCode=$v2['code'];
                    break;
                }
            }
        }
        //找出城市的code
        if(!isset($data[$provinceCode])){
            return $area;
        }
        $cityCode="";
        foreach($data[$provinceCode] as $k=>$v){
            if($v==$cityname){
                $cityCode=$k;
                break;
            }
        }
        //找出区域
        if(!isset($data[$cityCode])){
            return $area;
        }
        foreach($data[$cityCode] as $k=>$v){
            $area[]=['label'=>$v,'code'=>$k];
        }
        return $area;
    }
    public function add()
    {
        $param=$this->request->param();
        try{
            $param['deerhome_user_id']=$this->_user['id'];
            (new ShopApplyModel)->allowField(true)->save($param);
        }catch(\Exception $e){
            $this->err($e->getMessage());
        }
        $this->ok();
    }
}
